using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using SlasheneFramework;


namespace schemas.microsoft.com.developer.msbuild.Item2003
{
	public enum Position
	{
		After,
		Before
	}
	public partial class Project
	{
		public ImportType CommonImport
		{
			get
			{
				return Import.First(i => i.Project.Contains("Microsoft.CSharp.targets"));
			}
		}
	}

	public partial class ImportType
	{
		/// <summary>
		/// 
		/// </summary>
		/// <param name="projectPath"></param>
		/// <param name="position"></param>
		/// <returns>Is modified</returns>
		public bool EnsureHasSiblingImport(string projectPath, Position position)
		{
			var parentProject = this.Query.Ancestors<Project>().First();
			var imports = parentProject.Import;
			if(!parentProject.Import.Any(i => i.Project.Equals(projectPath)))
			{
				var newImport = new ImportType()
				{
					Project = projectPath
				};
				if(position == Position.After)
					imports.Insert(imports.IndexOf(this) + 1, newImport);
				else
					imports.Insert(imports.IndexOf(this), newImport);
				return true;
			}
			return false;
		}

		/// <summary>
		/// 
		/// </summary>
		/// <param name="file"></param>
		/// <param name="position"></param>
		/// <returns>Is modified</returns>
		public bool EnsureHasImportToFile(string toFile, string directory, Position position)
		{
			string relativePath = PathUtil.RelativePathTo(new DirectoryInfo(directory).FullName, new FileInfo(toFile).FullName);
			return EnsureHasSiblingImport(relativePath, position);
		}
	}
}
